<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>用户管理</title>
		<link rel="stylesheet" href="/static/layui/css/layui.css" />
<link rel="stylesheet" href="/static/ui/css/ui.css" />
	</head>
	<body class="pear-container">
		
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="set-table" lay-filter="set-table"></table>
			</div>
		</div>

		<script type="text/html" id="set-toolbar">
			<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
		        <i class="layui-icon layui-icon-add-1"></i>
		        新增
		    </button>
		</script>
		<script type="text/html" id="set-bar">
			<button class="pear-btn pear-btn-warming pear-btn-sm" lay-event="set">
				<i class="layui-icon layui-icon-util"></i>设置</button>
		</script>
		<script type="text/html" id="set-enable">
			<input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="set-enable" {{ d.enable== 1 ? 'checked' : '' }} />
		</script>
		<script type="text/html" id="edit-bar">
			<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit">
				<i class="layui-icon layui-icon-edit"></i>
			</button>
			<button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove">
				<i class="layui-icon layui-icon-delete"></i>
			</button>

		</script>
        <script src="../config.js"></script>
		<script src="/static/layui/layui.js"></script>
		<script src="/static/ui/ui.js"></script>
		<script>
			layui.use(['table', 'form', 'jquery','common'], function() {
				let table = layui.table;
				let form = layui.form;
				let $ = layui.jquery;
				let common = layui.common;

				let MODULE_PATH = "./";

				let cols = [
					[{
						title: '名称',
						field: 'name',
						align: 'center'
					}, {
						title: '标志',
						field: 'key',
						align: 'center'
					}, {
						title: '状态',
						toolbar: '#set-enable',
						align: 'center'
					},
					{
						title: '操作',
						toolbar: '#edit-bar',
						align: 'center'
					},
					{
						title: '配置',
						toolbar: '#set-bar',
						align: 'center',
						minWidth: 150,
						fixed: "right",
						align: "center"
					}
					]
				]

				table.render({
					elem: '#set-table',
					url: apiUrl + 'set/list',
				    headers: getHeader(),
					page: true,
                    parseData: function (res) {
                        return {
                            "code": res.code, //解析接口状态
                            "msg": res.message, //解析提示文本
                            "count": res.data.count, //解析数据长度
                            "data": res.data.list //解析数据列表
                        };
                    },
					cols: cols,
					skin: 'line',
                    toolbar: '#set-toolbar',
					defaultToolbar: [{
						title: '刷新',
						layEvent: 'refresh',
						icon: 'layui-icon-refresh',
					}, 'filter', 'print', 'exports']
				});
                table.on('tool(set-table)', function(obj) {
					if (obj.event === 'remove') {
						window.setRemove(obj);
					} else if (obj.event === 'edit') {
						window.setEdit(obj);
					} 
					 else if(obj.event === 'set') {
						window.setForm(obj);
					}
				});
				table.on('toolbar(set-table)', function(obj) {
					if (obj.event === 'add') {
						window.setAdd();
					} else if (obj.event === 'refresh') {
						window.refresh();
					}
				});

				form.on('switch(set-enable)', function (data) {
					let v = 1;
					if (data.elem.checked) v = 0;
					let postData = {
						id : data.value,
						status : v
					};
					_post(layui, 'set/enable', postData, res => {
						//console.log(res)
					},err => {
						$(data.elem).prop('checked', false);
						form.render('checkbox');
					})
				});
				

				window.setEdit = function(obj) {
					layer.open({
						type: 2,
						title: '修改配置-' + obj.data.name,
						offset: 'rt',
                        area: [
                            '80%', '100%'
                        ],
                        anim: 1,
                        maxmin: true,
						content: MODULE_PATH + 'edit.html?id=' + obj.data.id
					});
				}
				window.setForm = function(obj) {
					layer.open({
						type: 2,
						title: '修改配置-' + obj.data.name,
						offset: 'rt',
                        area: [
                            '80%', '100%'
                        ],
                        anim: 1,
                        maxmin: true,
						content : MODULE_PATH + 'set.html?id=' + obj.data.id
					});
				}
				window.setRemove = function(obj){
		            layer.confirm('确定要删除该配置吗', {icon: 3, title:'提示'}, function(index){
		                layer.close(index);
		                _post(layui, 'set/delete',{id : obj.data.id}, res => {
							layer.msg('删除成功',{icon:1,time:1000},() => {
								obj.del();
							});
						}, err => {
							//layer.msg('删除失败',{icon:2,time:1000});
						})
		            });
		        }
                window.setAdd = function(obj) {
					layer.open({
						type: 2,
						title: '添加配置',
						offset: 'rt',
                        area: [
                            '80%', '100%'
                        ],
                        anim: 1,
                        maxmin: true,
						content: MODULE_PATH + 'add.html'
					});
				}
				window.refresh = function(param) {
					table.reload('set-table');
				}
				
			})
		</script>
	</body>
</html>
